package com.smartalarmclock.UI;

import java.util.ArrayList;
import java.util.HashMap;

import com.smartalarmclock.Logic.AcManager;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.ITimeFrame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class ScheduleActivityForProfile extends Activity {
	
	IProfile m_Profile = null;
	private int m_ProfileId;
	@Override
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_schedule_screen);
        Bundle bundle = this.getIntent().getExtras();
        m_ProfileId = bundle.getInt(Messages.PROFILE_DETAILS_PROFILE_ID);
    	m_Profile = AcManager.GetInstance().GetProfileDetails(this, m_ProfileId);
    	
    	// set listener for buttons
        final Button backButton = (Button)findViewById(R.id.button_back);
        backButton.setOnClickListener(backButtonClickListener);
        
        final Button newProfileButton = (Button)findViewById(R.id.button_new);
        newProfileButton.setOnClickListener(newScheduleButtonClickListener);
        
        refreshScheduleList();
    }
	    
    private OnItemClickListener scheduleListItemClickListener = new OnItemClickListener() {
     	private static final String TAG = "OnItemClickListener";

        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        {
    	    Log.v(TAG, "Clicked on profile (Ofer)");

        	final ITimeFrame item = (ITimeFrame)parent.getItemAtPosition(position);
          
        	// start schedule details activity for editing schedule 
      		Intent newScheduleActivityIntent = new Intent(ScheduleActivityForProfile.this, CreateNewScheduleActivity.class);
      		newScheduleActivityIntent.putExtra(Messages.SCHEDULE_IS_NEW, false);
      		newScheduleActivityIntent.putExtra(Messages.SCHEDULE_TIME_FRAME_ID, item.GetId());
      		newScheduleActivityIntent.putExtra(Messages.PROFILE_DETAILS_PROFILE_ID, m_Profile.GetId());
      		startActivity(newScheduleActivityIntent);
      		
      		refreshScheduleList();
        }
    };
    
    private OnClickListener newScheduleButtonClickListener = new OnClickListener()
    {
    	@Override
        public void onClick(View v)
        {   
    		
        	// start schedule details activity for a new schedule 	    		
        	Intent newScheduleActivityIntent = new Intent(v.getContext(), CreateNewScheduleActivity.class);
        	newScheduleActivityIntent.putExtra(Messages.SCHEDULE_IS_NEW, true);
        	newScheduleActivityIntent.putExtra(Messages.SCHEDULE_TIME_FRAME_ID, -1);
        	newScheduleActivityIntent.putExtra(Messages.PROFILE_DETAILS_PROFILE_ID, m_Profile.GetId());
        	startActivity(newScheduleActivityIntent);
        	
        	refreshScheduleList();
        }
    };
    
    @Override
    protected void onResume()
    {
    	Log.v("Schedule", "Now entering Schedule onResume");
    	refreshScheduleList();
    	super.onResume();
    }
    
    private void refreshScheduleList()
    {
    	Log.v("Schedule", "Now entering refreshScheduleList");
    	m_Profile = AcManager.GetInstance().GetProfileDetails(this, m_ProfileId);
    	// set schedule list adapter and items on-click listener
    	final ListView scheduleListView = (ListView)findViewById(R.id.listView_list);
		scheduleListView.setAdapter(new ScheduleArrayAdapter(this, m_Profile));
        scheduleListView.setOnItemClickListener(scheduleListItemClickListener);
    }
    
    private OnClickListener backButtonClickListener = new OnClickListener()
    {
        public void onClick(View v)
        {   
             finish();
        }
    };
    
    private class ScheduleArrayAdapter extends ArrayAdapter<ITimeFrame>
    {
    	private Context m_Context = null;
    	HashMap<ITimeFrame, Integer> m_IdMap = null;
    	
    	public ScheduleArrayAdapter(Context context, IProfile profile)
    	{
    		super(context, R.layout.main_menu_item_details_view, AcManager.GetInstance().GetTimeFramesForProfile(context, m_Profile));
    		m_Context = context;
    		
    		updateScheduleData();
    	}
    	
    	public View getView(int position, View convertView, ViewGroup parent) {
    		ViewHolder holder;
    		if (convertView == null) {
    			convertView = LayoutInflater.from(m_Context).inflate(R.layout.schedule_list_item, null);
    			holder = new ViewHolder();
    			holder.txt_itemName = (TextView)convertView.findViewById(R.id.schedule_list_item_text);
    			holder.itemImage = (ImageView)convertView.findViewById(R.id.schedule_list_item_image);

    			convertView.setTag(holder);
    		} else {
    			holder = (ViewHolder) convertView.getTag();
    		}
    		
    		String timeToDisplay = getItem(position).GetStartTime().toString() + "-\n" + getItem(position).GetEndTime().toString();
    		holder.txt_itemName.setText(timeToDisplay);
    		holder.itemImage.setImageResource(R.drawable.profile);

    		return convertView;
    	}
    	
    	private void updateScheduleData()
    	{
    		ArrayList<ITimeFrame> scheduleList = AcManager.GetInstance().GetTimeFramesForProfile(m_Context, m_Profile);
    		m_IdMap = new HashMap<ITimeFrame, Integer>();
    		// set profiles in id-map
    		int i = 0;
    		for (ITimeFrame timeFrameItem : scheduleList)
    		{
    			m_IdMap.put(timeFrameItem, i++);
    		}
    	}
    	
    	@Override
    	public long getItemId(int position)
    	{
    		ITimeFrame item = getItem(position);
    		return m_IdMap.get(item);
    	}
    	
    	@Override
    	public boolean hasStableIds()
    	{
    		return true;
    	}
    	
    	@Override
    	public void notifyDataSetChanged()
    	{
    		updateScheduleData();
    		super.notifyDataSetChanged();
    	}
    }
    
    static class ViewHolder
    {
		TextView txt_itemName;
		ImageView itemImage;
	}

}
